package jltx.fragmentpageframework.wetchat;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.jltx.support.SupportAppCompatActivity;
import cn.jltx.support.SupportFragment;
import cn.jltx.utils.ViewHelper;
import cn.jltx.utils.XqToast;
import jltx.fragmentpageframework.R;
import jltx.fragmentpageframework.wetchat.adapter.WetChatTabPagerAdapter;
import jltx.fragmentpageframework.wetchat.fragment.WetChatContactFragment;
import jltx.fragmentpageframework.wetchat.fragment.WetChatFindFragment;
import jltx.fragmentpageframework.wetchat.fragment.WetChatMessageFragment;
import jltx.fragmentpageframework.wetchat.fragment.WetChatProfileFragment;
import jltx.fragmentpageframework.widget.BottomBarTabV2;
import jltx.fragmentpageframework.widget.BottomBarV2;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/17.
 */

public class WetChatActivityV2 extends SupportAppCompatActivity{
    public static final String TAG = "WetChatActivity";

    private static final int FRAGMENT_SIZE = 4;
    private List<WetChatTabPagerAdapter.TabFragmentInfo> fragmentList;

    private Toolbar toolbar;
    private BottomBarV2 bottomBarV2;
    private ViewPager viewPager;
    private WetChatTabPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wetchat2);
        toolbar = ViewHelper.findViewById(this,R.id.toolbar_wetchat);
        toolbar.setTitle(R.string.wechat_title);
        setSupportActionBar(toolbar);
        viewPager = ViewHelper.findViewById(this,R.id.viewpager_wetchat);
        bottomBarV2 = ViewHelper.findViewById(this,R.id.bottom_bar);
        if(savedInstanceState == null){
            fragmentList = new ArrayList<>();

            String tabTitle = this.getResources().getString(R.string.wetchat_msg);
            WetChatMessageFragment msgFragment = WetChatMessageFragment.newInstance(tabTitle,fragmentList.size());
            WetChatTabPagerAdapter.TabFragmentInfo fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(msgFragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_weixin_normal,R.drawable.wetchat_weixin_pressed).setIconBottomText(tabTitle));

            tabTitle = this.getResources().getString(R.string.wetchat_contact);
            WetChatContactFragment contactFragment = WetChatContactFragment.newInstance(tabTitle,fragmentList.size());
            fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(contactFragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_contact_normal,R.drawable.wetchat_contact_pressed).setIconBottomText(tabTitle));

            tabTitle = this.getResources().getString(R.string.wetchat_find);
            WetChatFindFragment findFragment = WetChatFindFragment.newInstance(tabTitle,fragmentList.size());
            fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(findFragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_find_normal,R.drawable.wetchat_find_pressed).setIconBottomText(tabTitle));


            tabTitle = this.getResources().getString(R.string.wetchat_profile);
            WetChatProfileFragment profileFragment = WetChatProfileFragment.newInstance(tabTitle,fragmentList.size());
            fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(profileFragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_profile_normal,R.drawable.wetchat_profile_pressed).setIconBottomText(tabTitle));


        }else {
            fragmentList = new ArrayList<>();
            String tabTitle = this.getResources().getString(R.string.wetchat_msg);
            SupportFragment fragment = findFragment(WetChatMessageFragment.class);
            WetChatTabPagerAdapter.TabFragmentInfo fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(fragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_weixin_normal,R.drawable.wetchat_weixin_pressed).setIconBottomText(tabTitle));

            tabTitle = this.getResources().getString(R.string.wetchat_contact);
            fragment = findFragment(WetChatContactFragment.class);
            fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(fragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_contact_normal,R.drawable.wetchat_contact_pressed).setIconBottomText(tabTitle));


            tabTitle = this.getResources().getString(R.string.wetchat_find);
            fragment = findFragment(WetChatFindFragment.class);
            fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(fragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_find_normal,R.drawable.wetchat_find_pressed).setIconBottomText(tabTitle));

            tabTitle = this.getResources().getString(R.string.wetchat_profile);
            fragment = findFragment(WetChatProfileFragment.class);
            fragmentInfo = new WetChatTabPagerAdapter.TabFragmentInfo(fragment,tabTitle,fragmentList.size());
            fragmentList.add(fragmentInfo);
            bottomBarV2.addItem(new BottomBarTabV2(this).setIconMaskDrawable(R.drawable.wetchat_profile_normal,R.drawable.wetchat_profile_pressed).setIconBottomText(tabTitle));

        }

        pagerAdapter = new WetChatTabPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(pageChangeListener);
        bottomBarV2.setOnTabSelectedListener(onTabSelectedListener);
        viewPager.setOffscreenPageLimit(fragmentList.size());
    }

    private BottomBarV2.OnTabSelectedListener onTabSelectedListener = new BottomBarV2.OnTabSelectedListener() {
        @Override
        public void onTabSelected(int position, int prePosition) {
            viewPager.setCurrentItem(position,false);
        }

        @Override
        public void onTabUnselected(int position) {
        }

        @Override
        public void onTabReselected(int position) {

        }
    };

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            bottomBarV2.onTabPageScrolled(position,position+1,positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            bottomBarV2.setCurrentPosition(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {//MENU键
            if(toolbar != null){
                toolbar.showOverflowMenu();
                return true;
            }

        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_wetchat_menu,menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                }
            }
        }
        return super.onPrepareOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            XqToast.showToastShortMsg(this,"返回按钮");
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * 设置menu显示icon
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu)
    {

        if (featureId == Window.FEATURE_ACTION_BAR && menu != null)
        {
            if (menu.getClass().getSimpleName().equals("MenuBuilder"))
            {
                try
                {
                    Method m = menu.getClass().getDeclaredMethod(
                            "setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }
}
